package com.zjun.demo.experimentaldemo.udp

import android.os.Bundle
import android.view.View
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.udp.call.ICallBackMsg
import com.zjun.demo.experimentaldemo.udp.call.IClientLaunchCallBack
import com.zjun.demo.experimentaldemo.udp.call.IServerLaunchCallBack
import com.zjun.demo.experimentaldemo.udp.client.UdpClient
import com.zjun.demo.experimentaldemo.udp.server.UdpServer
import com.zjun.demo.experimentaldemo.udp.server.UdpServerHandler
import io.netty.channel.Channel
import io.netty.channel.ChannelHandlerContext
import io.netty.util.internal.ThreadLocalRandom
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.concurrent.thread


/**
 * 通过Netty建立UDP的通讯示例
 */
class UdpConnectActivity : AppCompatActivity(),
    IServerLaunchCallBack,
    IClientLaunchCallBack {

    private val proverbs = arrayOf(
        "成语", "谚语", "你好"
    )

    private val logs = CopyOnWriteArrayList<String>()

    private lateinit var mTvResponse:TextView

    private lateinit var mScroll:ScrollView

    private val serverCallBackMsg = object:ICallBackMsg{

        override fun callMsg(message: String) {
            logs.add("服务端收到:$message")
            log()
        }

    }

    private val clientCallBackMsg = object:ICallBackMsg{

        override fun callMsg(message: String) {
            logs.add("客户端收到:$message")
            log()
        }

    }

    private val clientLaunchCallBack = object:IClientLaunchCallBack{

        override fun clientLaunchSuccess(ch: Channel) {
            logs.add("客户端启动成功------")
            log()
        }

        override fun clientLaunchFail(ctx: ChannelHandlerContext) {
            logs.add("客户端启动失败------")
            log()
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_udp_connect)
        mTvResponse = findViewById(R.id.tvResponse)
        mScroll = findViewById(R.id.scroll)
        UdpServer.instance.iServerLaunchCallBack = this
        UdpServer.instance.setCallBackMsgListener(serverCallBackMsg)
        UdpClient.instance.setCallBackMsgListener(clientCallBackMsg)
        UdpClient.instance.setClientLaunchCallBack(clientLaunchCallBack)
    }

    fun startService(view: View) {
        thread {
            UdpServer.instance.start()
        }
    }

    fun connectService(view: View) {
        thread {
            UdpClient.instance.start()
        }
    }

    fun sendMsg(view: View) {
        val msg = nextProverb()
        UdpClient.instance.sendMsg(msg)
    }

    override fun launchSuccess(ch: Channel) {
        logs.clear()
        logs.add("服务启动成功------")
        log()
    }

    override fun launchFail(ctx: ChannelHandlerContext) {
        logs.clear()
        logs.add("服务启动失败------")
        log()
    }

    override fun clientLaunchSuccess(ch: Channel) {
        logs.add("客户端启动成功------")
        log()
    }

    override fun clientLaunchFail(ctx: ChannelHandlerContext) {
        logs.add("客户端启动失败------")
        log()
    }

    private fun log(){
        runOnUiThread {
            mTvResponse.text = logs.joinToString("\n")
            mScroll.fullScroll(View.FOCUS_DOWN)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        UdpClient.instance.destroy()
        UdpServer.instance.destroy()
    }

    private fun nextProverb(): String {
        val nextInt = ThreadLocalRandom.current().nextInt(proverbs.size)
        return proverbs[nextInt]
    }


}